<template>  
  <div class="app-appNewsCategory-edit">
    <!-- 添加或修改文章分类对话框 -->
    <el-dialog v-model="isShowDialog" width="800px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.app-appNewsCategory-edit .el-dialog', '.app-appNewsCategory-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'文章分类'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">        
        <el-form-item label="名称" prop="cateName">
          <el-input v-model="formData.cateName" placeholder="请输入名称" />
        </el-form-item>        
        <el-form-item label="父ID" prop="pid">
          <el-cascader
            v-model="formData.pid"
            placeholder="请选择"
            :options="appNewsCategoryOptions"
            filterable
            clearable
            :props="{ label: 'cateName',value: 'id',checkStrictly: true,emitPath: false }"
          />
        </el-form-item>       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listAppNewsCategory,
  getAppNewsCategory,
  delAppNewsCategory,
  addAppNewsCategory,
  updateAppNewsCategory,  
} from "/@/api/app/appNewsCategory";
import {
  AppNewsCategoryTableColumns,
  AppNewsCategoryInfoData,
  AppNewsCategoryTableDataState,
  AppNewsCategoryEditState
} from "/@/views/app/appNewsCategory/list/component/model"
export default defineComponent({
  name:"ApiV1AppAppNewsCategoryEdit",  
  props:{    
    // 文章分类树选项
    appNewsCategoryOptions :{
      type:Array,
      default:()=>[]
    },    
  },  
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive<AppNewsCategoryEditState>({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        cateName: undefined,        
        pid: undefined,        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "ID不能为空", trigger: "blur" }
        ],        
        cateName : [
            { required: true, message: "名称不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = (row?: AppNewsCategoryInfoData) => {
      resetForm();
      if(row) {
        getAppNewsCategory(row.id!).then((res:any)=>{
          const data = res.data;          
          data.pid = parseInt(data.pid)          
          state.formData = data;
      })
    }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addAppNewsCategory(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('appNewsCategoryList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updateAppNewsCategory(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('appNewsCategoryList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        cateName: undefined,        
        pid: undefined,        
      }      
    };    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
  .kv-label{margin-bottom: 15px;font-size: 14px;}
  .mini-btn i.el-icon{margin: unset;}
  .kv-row{margin-bottom: 12px;}
</style>